package com.hilinos.common.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RespVo<T> {

    /**
     * 默认失败
     */
    public static final int CODE_FAIL = 2;
    /**
     * 默认成功
     */
    public static final int CODE_SUCCESS = 1;

    private int code;
    private String msg;
    private String error;
    private String time;
    private T data;

    /**
     * 获取失败返回值
     */
    public static <T> RespVo<T> getSuccess(T data) {
        return get(CODE_SUCCESS, "成功", data, "成功");
    }

    /**
     * 获取返回值
     *
     * @param code 返回的状态码
     * @param msg  返回状态描述
     * @param data 返回的数据
     * @param <T>  数据对应的类型
     */
    public static <T> RespVo<T> get(int code, String msg, T data, String error) {
        RespVo<T> respVo = new RespVo<>();
        respVo.setCode(code);
        respVo.setMsg(msg);
        respVo.setError(error);
        respVo.setData(data);
        respVo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return respVo;
    }

    /**
     * 获取返回值
     *
     * @param code 返回的状态码
     * @param msg  返回状态描述
     */
    public static <T> RespVo<T> get(int code, String msg) {
        return get(code, msg, null, "");
    }
}
